
import request from "./request.js"
import { getToken, setToken, removeToken } from './myCookie.js'


// 登录方法
export function login() {
	return new Promise((resolve, reject) => {
		wx.getUserProfile({
			desc:"获取用户头像，名称！",
			success: response=> {
				console.log(response);
				getApp().globalData.userName = response.userInfo.nickName;
				getApp().globalData.headPhoto = response.userInfo.avatarUrl;
				getApp().globalData.agree = "yes";
				
				wx.login({
				  success (res) {
					if (res.code) {
					  //发起网络请求
					  const P = request({
						url: '/onLogin?code=' + res.code,
						headers: {
						  isToken: false
						},
						method: 'get',
					  })
					  P.then( res => {
						  setToken(res.token);
					  })
					} else {
					  console.log('登录失败！' + res.errMsg)
					}
				  }
				});
				
				wx.showToast({
					title: '登录成功',
					icon: "success"
				})
				getApp().globalData.isLogin = true;
		
			},
			fail: rej=> {
				console.error(rej);
				getApp().globalData.agree = "no";
			}
		});
		resolve();
	})
  
  
}

// 获取用户详细信息
export function getInfo() {
  return request({
    url: '/getInfo',
    method: 'get'
  })
}

// 退出方法
export function logout() {
  return request({
    url: '/logout',
    method: 'post'
  })
}

// 获取验证码
export function getCodeImg() {
  return request({
    url: '/captchaImage',
    headers: {
      isToken: false
    },
    method: 'get',
    timeout: 20000
  })
}